<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogTips"
    width="500"
    :before-close="handleClose"
  >
    <div v-if="visibleNum == 1">
      <el-select v-model="userState" placeholder="请选择">
        <el-option label="正常" :value="1" />
        <el-option label="停课" :value="0" />
        <el-option label="延期考试" :value="2" />
      </el-select>
      <!-- 停课日期 -->
      <div style="margin-top: 15px">
        <DatePicker
          placeholder="请选择停课日期"
          v-model="suspensionClassDate"
          v-if="userState == 0"
        ></DatePicker>
      </div>
    </div>
    <el-select
      filterable
      v-model="otherClassId"
      placeholder="请选择班级名称"
      v-else-if="visibleNum == 2"
    >
      <el-option
        :label="item.className"
        :value="item._id"
        v-for="(item, index) in otherClassOptions"
        :key="index"
      >
        <el-row :gutter="20">
          <el-col :span="20">
            <div>{{ item.className }}</div>
          </el-col>
          <el-col :span="2">
            <div v-if="item.status == 0" style="color: #e99d42">待开班</div>
            <div v-else-if="item.status == 1" style="color: #54c076">开班</div>
            <div v-else>结业</div>
          </el-col>
          <el-col :span="2">
            <div>{{ item.classStudentsNum }}/{{ item.classNum }}</div>
          </el-col>
        </el-row>
      </el-option>
    </el-select>

    <!-- 批次号 -->
    <el-input
      v-model="declarationBatchNumber"
      v-else-if="visibleNum == 5"
      placeholder="请输入申请批次号"
    />

    <!-- 考试性质 -->
    <el-select
      v-model="examType"
      placeholder="请选择考试性质"
      v-else-if="visibleNum == 6"
    >
      <el-option label="正考" :value="1" />
      <el-option label="补考" :value="0" />
    </el-select>

    <!-- 考试日期 -->
    <DatePicker v-else-if="visibleNum == 7" v-model="examDate"></DatePicker>

    <!-- 报名所属 -->
    <dictSelect
      v-model="affiliation"
      v-else-if="visibleNum == 9"
      dictCode="registration_affiliation"
    ></dictSelect>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="loading">
          确 认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
const { proxy } = getCurrentInstance();
const emit = defineEmits(["fatherMethods"]);

let dialogVisible = ref(false);
let visibleNum = ref(0);
let loading = ref(false);
let selectArr = ref([]); //选中的学生
let declarationBatchNumber = ref("");
let examType = ref(null); //考试性质
let examDate = ref(null); //考试日期

let dialogTips = computed(() => {
  switch (visibleNum.value) {
    case 1:
      return "修改学员状态";
      break;
    case 2:
      return "调至其他班";
      break;
    case 5:
      return "学员申报";
      break;
    case 6:
      return "考试性质";
      break;
    case 7:
      return "考试日期";
      break;
    case 9:
      return "报名所属";
      break;
    default:
      break;
  }
});

function open(e, arr) {
  selectArr.value = arr;
  visibleNum.value = e;
  dialogVisible.value = true;
}

async function submit() {
  try {
    loading.value = true;
    for (let index = 0; index < selectArr.value.length; index++) {
      const element = selectArr.value[index];
      let data = { _id: element };
      switch (visibleNum.value) {
        case 5:
          data["declarationBatchNumber"] = declarationBatchNumber.value;
          break;
        case 6: //考试性质
          if (examType.value == null)
            return proxy.$utils.showToast("请选择考试性质", 0);
          data["examType"] = examType.value;
          break;
        case 7: //考试日期
          if (examDate.value == null)
            return proxy.$utils.showToast("请选择考试日期", 0);
          data["examDate"] = examDate.value;
          break;
        default:
          break;
      }
      await proxy.$apis.classStudent({
        type: "update",
        data,
      });
    }
    proxy.$utils.showToast("操作成功");
    handleClose();
  } catch (error) {
  } finally {
    loading.value = false;
  }
}

function handleClose() {
  emit("fatherMethods");
  visibleNum.value = 0;
  selectArr.value = [];
  examDate.value = null;
  declarationBatchNumber.value = "";
  examType.value = null;
  dialogVisible.value = false;
}

defineExpose({ open });
</script>

<style lang="scss" scoped>
.container {
}
</style>
